package com.ad.pet.app.login.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ad.pet.R;
import com.ad.pet.models.remote.login.CategroyCountry;
import com.ad.pet.models.remote.login.Country;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Created by SEELE on 2018/3/21.
 */

public class CountryAdapter extends BaseQuickAdapter<CategroyCountry,BaseViewHolder> {
OnItemSelectCityLinstener onItemSelectCityLinstener;
    public CountryAdapter(@Nullable List<CategroyCountry> data) {
        super(R.layout.item_country_layout,data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, CategroyCountry item) {
        TextView tvTitle = helper.getView(R.id.tv_title);
        TextView tvChar = helper.getView(R.id.tv_char);
        RecyclerView recyclerCity = helper.getView(R.id.recycler_city);
        if ("location".equals(item.getInitial())||"hot".equals(item.getInitial())){
            tvTitle.setVisibility(View.VISIBLE);
            tvChar.setVisibility(View.GONE);
            tvTitle.setText("location".equals(item.getInitial())?"定位城市":"热门城市");
        }else {
            tvTitle.setVisibility(View.GONE);
            tvChar.setVisibility(View.VISIBLE);
            tvChar.setText(item.getInitial());
        }
        CityAdapter cityAdapter = new CityAdapter(item.getList());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext,3);
        recyclerCity.setLayoutManager(gridLayoutManager);
        recyclerCity.setAdapter(cityAdapter);

        cityAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (onItemSelectCityLinstener!=null){
                    onItemSelectCityLinstener.onItemClick(helper.getLayoutPosition(),position);
                }
            }
        });
    }

    public int getScrollPosition(String character) {

        for (int i = 0; i < mData.size(); i++) {
//            !"location".equals(mData.get(i).getInitial()))&&mData.get(i).getList()!=null&&mData.get(i).getList().size()!=0&&(mData.get(i).getList().get(0).getFirst_letter()+"")
            if ((mData.get(i).getInitial().toLowerCase().equals(character))){
                return i;
            }
        }
        return -1; // -1不会滑动
    }

    class CityAdapter extends BaseQuickAdapter<Country,BaseViewHolder>{

        public CityAdapter( @Nullable List<Country> data) {
            super(R.layout.item_city_layout, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Country item) {
            TextView tvCity = helper.getView(R.id.tv_city);
            tvCity.setText(item.getCity_name());
        }
    }

    public void setOnItemSelectCityLinstener(OnItemSelectCityLinstener onItemSelectCityLinstener) {
        this.onItemSelectCityLinstener = onItemSelectCityLinstener;
    }

    public interface OnItemSelectCityLinstener{
        void onItemClick(int location,int position);
    }

}
